package com.hbt.word_provider.controller;

import com.hbt.api.entity.CommonResult;
import com.hbt.api.entity.StaticBean;
import com.hbt.api.entity.Word;
import com.hbt.api.entity.WordsCondition;
import com.hbt.word_provider.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@RequestMapping("word_provider")
public class WordProviderController {
    @Autowired
    WordService wordService;

    @PostMapping("del_word")
    public CommonResult<String> delWord(int wordid){
        int count = wordService.delWord(wordid);
        CommonResult<String> commonResult = new CommonResult<String>();
        commonResult.setCode(200);
        commonResult.setMsg("删除了单词和特征记录条数共:"+count);
        commonResult.setT("删除了单词和特征记录条数共:"+count);
        return commonResult;
    }

    @PostMapping("get_progress")
    public CommonResult<StaticBean> getProgress(int userid){
        StaticBean staticBean = wordService.getProgress(userid);
        CommonResult<StaticBean> commonResult = new CommonResult<StaticBean>();
        commonResult.setT(staticBean);
        commonResult.setMsg("获取统计进度条成功");
        commonResult.setCode(200);
        System.out.println("controller get_progress end");
        return commonResult;
    }

    @PostMapping("find_similar")
    public CommonResult<List<Word>> findSimilar(String src, int rate, @RequestBody List<Word> words){
        List<Word> word_reuslt = wordService.findSimilar(src, rate, words);
        CommonResult<List<Word>> result = new CommonResult<List<Word>>();
        result.setCode(200);
        result.setMsg("获取相似单词成功, 数量："+word_reuslt.size());
        result.setT(word_reuslt);
        return result;
    }

    @PostMapping("update_word")
    public CommonResult<String> updateWord(@RequestBody Word word){
        System.out.println("controller word:"+word);
        int count = 0;
        count = wordService.updateWord(word);
        CommonResult<String> result = new CommonResult<String>();
        result.setCode(200);
        result.setMsg(count > 0?"更新word成功":"更新word失败");
        result.setT(count > 0?"更新word成功":"更新word失败");
        return result;
    }

    @PostMapping("get_word_by_wordid")
    public Word getWordByWordid(int wordid){
        System.out.println("controller wordid:"+wordid);
        return wordService.getWordByWordid(wordid);
    }

    @PostMapping("get_word_by_condition")
    public List<Word> getWordByCondition(@RequestBody WordsCondition wordsCondition){
        System.out.println("controller wordcondition:"+wordsCondition);
        return wordService.getWordByCondition(wordsCondition);
    }


    @PostMapping("add_word")
    public CommonResult<String> addWord(@RequestBody Word word){
        int yes = 0;
        //先判断数据库中是否有userid和src相同的数据行
        if (!wordService.hasSameWord(word.getUserid(), word.getSrc())){
            //若没有则执行插入操作
            yes = wordService.addWord(word);
        }
        CommonResult<String> result = new CommonResult<String>();
        result.setCode(200);
        result.setMsg(yes > 0?"插入word成功":"插入失败");
        result.setT(yes > 0?"插入word成功":"插入失败");

        return result;
    }


    @PostMapping("fanyi")
    public CommonResult fanyi(String src){
        System.out.println("enter fanyi controller src:"+src);
        String tgt = wordService.fanyi(src);
        CommonResult<String> commonResult = new CommonResult<String>();
        if (tgt == null || tgt.equals("")){
            commonResult.setT("null");
            commonResult.setMsg("翻译出现错误");
            commonResult.setCode(0);
        }else{
            commonResult.setT(tgt);
            commonResult.setMsg("成功获取翻译");
            commonResult.setCode(1);
        }
        return commonResult;
    }//fanyi end



}
